package com.company3;

import java.util.Arrays;
import java.util.List;

public class countchar {
    public static void main(String[] args) {
        String str = "ABCD123!@#$%ab";
        count(str);
    }
    static void count(String str){
        List<String> a = Arrays.asList(str.split(""));
        //正则匹配大写，小写，数字
        String Upper = "[A-Z]";
        String Lower = "[a-z]";
        String Num = "[0-9]";
        //计数
        int countupper = 0;
        int countlower = 0;
        int countnum = 0;
        int countother = 0;
        //定义可变字符串为打印准备
        StringBuilder upper = new StringBuilder();
        StringBuilder lower = new StringBuilder();
        StringBuilder num = new StringBuilder();
        StringBuilder other = new StringBuilder();
        for(String i :a){
            //片段是否是大写字母
            if (i.matches(Upper)){
                countupper+=1;
                upper.append(i);
            }else if (i.matches(Lower)){    //片段是否是小写字母
                countlower+=1;
                lower.append(i);
            }else if (i.matches(Num)){      //片段是否是数字
                countnum+=1;
                num.append(i);
            }else {                         //片段是否是其他字符
                countother+=1;
                other.append(i);
            }
        }
        //打印
        System.out.printf("大写字母%d个：\"%s\"；\n小写字母%d个：\"%s\"；\n数字%d个：\"%s\"；\n其他字符%d个：\"%s\"。\n",
                countupper, upper, countlower, lower, countnum, num, countother, other);
    }
}
